package com.nf.health.app.healthstate.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.nf.health.app.R;
import com.nf.health.app.healthstate.activity.AddHealthyPlanActivity;
import com.nf.health.app.healthstate.activity.MyPlanDetailsActivity;
import com.nf.health.app.adapter.OnMyClickListener;
import com.nf.health.app.adapter.SchemeCategoryAdapter;
import com.nf.health.app.fragment.IBaseFragment;
import com.nf.health.app.models.SchemeCaterogyList;
import com.nf.health.app.utils.ActivityUtils;
import com.nf.health.app.utils.ViewUtil;

import java.util.List;

public class MyHealthyVpFragment extends IBaseFragment implements OnMyClickListener{
	private ListView lvHealthy;
	public static final String KEY = "position";
	private SchemeCategoryAdapter adapter;
	private List<SchemeCaterogyList> categoryList;



	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		
		addViewFillInRoot(R.layout.activity_my_healthy_plan);
		lvHealthy = ViewUtil.getView(rootView, R.id.lv_healthy_plan);
		int position = getArguments().getInt(KEY);
		provider.requestSchemeList(AddHealthyPlanActivity.categories.get(position).getCategory() + "", "SchemeList");
		
		initListener();
	}
	
	private void initListener() {
		lvHealthy.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view,
					int position, long id) {
//				Intent intent = new Intent(getActivity(), MyPlanDetailsActivity.class);
//				intent.putExtra("schemeid", categoryList.get(position).getId());
//				startActivity(intent);
				Bundle bundle = new Bundle();
				bundle.putString("schemeid", categoryList.get(position).getId()+"");
				ActivityUtils.showActivity(getActivity(), MyPlanDetailsActivity.class, bundle);
			}
		});
	}

	@SuppressWarnings("unchecked")
	@Override
	public void handleActionSuccess(String actionName, Object object) {
		super.handleActionSuccess(actionName, object);
		if (actionName.equals("SchemeList")) {
			categoryList = (List<SchemeCaterogyList>) object;
			adapter = new SchemeCategoryAdapter(getActivity(), categoryList);
			lvHealthy.setAdapter(adapter);
			adapter.setOnMyClickListener(this);
			
		} else if (actionName.equals("addToUser")) {
			showToast("添加完成");
			getActivity().setResult(101);
			getActivity().finish();
		}
	}

	@Override
	public void onClick(int position, String flag) {
		provider.requestAddToUser(categoryList.get(position).getId()+"","addToUser");
	}
}
